﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace Typing_GUI.CLASS
{
   public class CBaiTapDuocLuu
    {
        
        #region "thuoc tinh"
            private int mabaitap;
            private int manguoidung;
            private string noidungdalam;
            private int sodiemdaco;
            private int thoigianconlai;
        #endregion

        #region "properties"
            public int Thoigianconlai
            {
                get { return thoigianconlai; }
                set { thoigianconlai = value; }
            }

            public int Sodiemdaco
            {
                get { return sodiemdaco; }
                set { sodiemdaco = value; }
            }

            public string Noidungdalam
            {
                get { return noidungdalam; }
                set { noidungdalam = value; }
            }

            public int Manguoidung
            {
                get { return manguoidung; }
                set { manguoidung = value; }
            }

            public int Mabaitap
            {
                get { return mabaitap; }
                set { mabaitap = value; }
            }
        #endregion

        #region "phuong thuc"

        public static bool deleteSavedlesson(int id,int userID)
        {
            string sql = "delete from BaiTapDuocLuu where MaNguoiDung="+userID.ToString()+ " and MaBaiTap=" + id.ToString();
            if (!DataProvider.ExecuteNonQuery(sql))
                return false;
            return true;
        }

        public bool createSavelesson()
        {
            string sql = "insert into BaiTapDuocLuu(MaBaiTap,MaNguoiDung,ThoiGianConLai,SoDiemDangCo,NoiDungDaLam) values";
            sql += "(" + this.mabaitap.ToString() + "," + this.manguoidung.ToString() + "," + this.thoigianconlai.ToString() + ",";
            sql += this.sodiemdaco.ToString() + ",'" + this.noidungdalam + "')";
            if (!DataProvider.ExecuteNonQuery(sql))
                return false;
            return true;
        }

        public bool updateSavelesson()
        {

            string sql = "update BaiTapDuocLuu set ";
            sql += "MaBaiTap=" + this.mabaitap.ToString();
            sql += ",ThoiGianConLai=" + this.thoigianconlai.ToString();
            sql += ",SoDiemDangCo=" + this.sodiemdaco.ToString();
            sql += ",NoiDungDaLam='" + this.noidungdalam + "'";
            sql += " where MaNguoiDung="+this.Manguoidung+" and  MaBaiTap=" + this.mabaitap.ToString();
            if (!DataProvider.ExecuteNonQuery(sql))
                return false;
            return true;
        }

        public static CBaiTapDuocLuu getSavedTextLesson(int userID)
        {
            CBaiTapDuocLuu result = new CBaiTapDuocLuu();
            string sql = "SELECT   BaiTapDuocLuu.*";
            sql += "FROM (BaiTap INNER JOIN  BaiTapDuocLuu ON BaiTap.MaBaiTap = BaiTapDuocLuu.MaBaiTap) where BaiTapDuocLuu.MaNguoiDung=" + userID.ToString() + " and BaiTap.LoaiBaiTap=1";
            DataTable dt = new DataTable();
            dt = DataProvider.ExecuteQuery(sql);
            if (dt.Rows.Count < 1)
                return null;
            result.mabaitap = int.Parse(dt.Rows[0]["MaBaiTap"].ToString());
            result.manguoidung = userID;
            result.noidungdalam = dt.Rows[0]["NoiDungDaLam"].ToString();
            result.sodiemdaco = int.Parse(dt.Rows[0]["SoDiemDangCo"].ToString());
            result.thoigianconlai = int.Parse(dt.Rows[0]["ThoiGianConLai"].ToString());
            return result;
        }

       public static CBaiTapDuocLuu getSavedAdventureLesson(int userID)
       {
           CBaiTapDuocLuu result = new CBaiTapDuocLuu();
           string sql = "SELECT   BaiTapDuocLuu.*";
           sql += "FROM (BaiTap INNER JOIN  BaiTapDuocLuu ON BaiTap.MaBaiTap = BaiTapDuocLuu.MaBaiTap) where BaiTapDuocLuu.MaNguoiDung=";
           sql+=userID.ToString()+" and baitap.loaibaitap=2";
           DataTable dt = new DataTable();
           dt = DataProvider.ExecuteQuery(sql);
           if (dt.Rows.Count < 1)
               return null;
           result.mabaitap = int.Parse(dt.Rows[0]["MaBaiTap"].ToString());
           result.manguoidung = userID;
           result.noidungdalam = dt.Rows[0]["NoiDungDaLam"].ToString();
           result.sodiemdaco = int.Parse(dt.Rows[0]["SoDiemDangCo"].ToString());
           result.thoigianconlai = int.Parse(dt.Rows[0]["ThoiGianConLai"].ToString());
           return result;
       } 

        public static bool deleteSavedTextLesson(int userID)
        {
            string sql = "delete from BaiTapDuocLuu where MaNguoiDung=" + userID.ToString();
            if (!DataProvider.ExecuteNonQuery(sql))
                return false;
            return true;
        }

       public static bool deleteLesson(int userID,int mabaitap)
       {
           string sql = "delete from BaiTapDuocLuu where MaNguoiDung=";
           sql+=userID.ToString()+" and mabaitap="+mabaitap.ToString();
           if (!DataProvider.ExecuteNonQuery(sql))
               return false;
           return true;
       }
        #endregion
    }
}
